<?php
/**
 * 
 * Enter description here ...
 * @author Zijian Wang
 */

class Permission_model extends CI_Model {

	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
	}

   /**
	*
	* to create the permission of a new photo.
	* @param  $f_type,  friend_type, which is checked in defined classes,for example,{train buddies,family,college 
	*                   friends}
	* @param  $p_id,  photo_ID
	* @param  $permit,  permission, wchich is checked in {visible,invisible}.
	* @return true/false
	*/

	
	function create_permission($f_type, $p_id, $permit)
	{
		if( !($f_type == "train buddies" or $f_type == "family" or $f_type == "college friends") 
			or !($permit == "visible" or $permit == "invisible"))
		{
			return false;
		}
		$sql = "INSERT INTO permission(f_type,p_id,permit) 
		VALUES ";
		$sql.= "('".$f_type."',".$p_id.",'".$permit."')";
		$result = $this->db->query($sql);
		return $result;
	}


	 /**
	*
	* to change the permission of a photo in existence.
	* @param  $f_type,  friend_type, which is checked in defined classes,for example,{train buddies,family,collge 
	*                   friends}
	* @param  $p_id,  photo_ID
	* @param  $permit,  permission, wchich is checked in {visible,invisible}.
	* @return true/false
	*/

	
	function change_permission($f_type, $p_id, $permit)
	{
		if( !($f_type == "train buddies" or $f_type == "family" or $f_type == "college friends") 
			or !($permit == "visible" or $permit == "invisible"))
		{
			return false;
		}
		$sql = "UPDATE permission SET permit = '".$permit."' WHERE f_type = '".$f_type."' AND p_id = ".$p_id;
		$result = $this->db->query($sql);
		return $result;
	}


   /**
	*
	* to check whether the permission matchs the checking.
	* @param  $f_type,  friend_type, which is checked in defined classes,for example,{train buddies,family,collge 
	*                   friends}
	* @param  $p_id,  photo_ID
	* @param  $permit,  permission, wchich is checked in {visible,invisible}.
	* @return true/false
	*/

	function check_permission($f_type, $p_id, $permit)
	{
		if( !($f_type == "train buddies" or $f_type == "family" or $f_type == "college friends") 
			or !($permit == "visible" or $permit == "invisible"))
		{
			return false;
		}
		$sql = "SELECT * FROM permission WHERE permit = '".$permit."' AND f_type = '".$f_type."' AND p_id = ".$p_id;
		$result = $this->db->query($sql);
		if(empty($result))
		{
			return false;
		}
		else return true;
	}



}


  

